package uk.gov.swmprobation.cplite;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SetFragment extends PreferenceFragment {
	
	Context context;

	public SetFragment(){
		context = MyApplication.getContext();
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        OnPreferenceChangeListener updatedNote = new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				preference.setSummary((CharSequence) newValue);
				return true;
			}
        	
        };
        
        Preference prefUserCode = findPreference("pref_user_code");
        prefUserCode.setSummary(sharedPref.getString("pref_user_code", "General Public"));
        prefUserCode.setOnPreferenceChangeListener(updatedNote);
        Preference prefWaitWifi = (Preference)findPreference("pref_wait_wifi");
        int wifiCode = Integer.parseInt(sharedPref.getString("pref_wait_wifi", "2"));
        prefWaitWifi.setSummary(SettingsActivity.wifiText(wifiCode));
        prefWaitWifi.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				preference.setSummary((CharSequence) SettingsActivity.wifiText(Integer.valueOf(newValue.toString())));
				return true;
			}
        	
        });
        
        EditTextPreference etp = (EditTextPreference) findPreference("user_email");
        String emailSummary = sharedPref.getString("user_email", "No email entered");
        if (!(emailSummary.length()>3)) emailSummary="No email entered";
	    etp.setSummary(emailSummary);
	    etp.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				String newEmailSummary = (String) newValue;
				if (!(newEmailSummary.length()>3)) newEmailSummary="No email entered";
				preference.setSummary(newEmailSummary);
				return true;
			}
        	
        });
        
        Preference prefResetHints = (Preference)findPreference("resetHints");
        prefResetHints.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				sharedPref.edit().putBoolean(SettingsActivity.submitHintTitle, true).commit();
				Toast.makeText(context, "All Hints will be shown", Toast.LENGTH_SHORT).show();
				return false;
			}
        	
        });
    }
	
}
